<template>
  <el-button type="primary" @click="handleRowEdit()"> 编辑</el-button>
  <lay-json-schema-form :model="form" :schema="schema1" ></lay-json-schema-form>
  <lay-button type="primary" @click="submit1">提交</lay-button>
</template>

<script>
import {ref, reactive, toRefs, onMounted} from 'vue';
import { layer } from '@layui/layer-vue'

import {getModelsColume, getModelsData, editModels} from '@/api/model'

import { useRouter } from 'vue-router'

export default {
  methods: {
    handleRowEdit() {
      // 编辑操作
      this.$router.push("/manage/form_edit?modelname="+this.modelname+"&id="+this.id);
    },
  },
  setup (){
    const router = useRouter();
    const modelname = router.currentRoute.value.query.modelname;
    const id = 12

    const state = reactive({
      form: new Object(),
      schema1: {
        tablename: {
          label: '编辑表',
          type: 'rate'
        },
      }
    })

    const getTableName = () => {
      getModelsData(modelname,id).then(res => {
        state.form["tablename"] = "编辑";
        let obj = res.data[0];
        for(let key in obj){
          state.form[key] = obj[key]
        }
      })
      getModelsColume(modelname).then(res => {
        for (let i = 0; i < res.data.length; i++) {
          let obj = res.data[i];
          state.schema1[obj.Field] = {
            label: obj.Comment,
            type: 'input',
            props: {
              type: 'text',
              placeholder: '请输入'+obj.Comment,
              readonly: 'readonly'
            }
          }
        }
      })
    }

   /**
     const form = reactive({
  code: 'HS123456',
  codename: '路途集团'
})
     */

    const submit1 = () => {
      editModels(modelname,state.form).then(res => {
        layer.msg('编辑成功', { time: 2000 });
        router.go(-1);
      })

    }

    onMounted(getTableName)

    return {
      ...toRefs(state),
      submit1,
      id,
      modelname
    }
  }
}
</script>